package com.cashmama.market.api.domain.service.actions;

import com.cashmama.market.api.domain.enums.UserProfileActionStatusEnum;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.utils.AppUtils;
import com.cashmama.market.api.infras.enums.ActionTypeEnums;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qianli.cashmama.product.facade.result.AppConfig;
import com.qianli.cashmama.product.facade.result.AppVo;
import com.qianli.user.ro.auth.UserAuthStatusRO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-24
 * Time: 14:53
 * <p>
 * 用于检查用户基本信息是否完整
 */
@Service
public class ActionListService {

    public static Map<ActionTypeEnums, BaseAction> actionListHandler = new HashMap<>();

    @Autowired
    private ApiUserService apiUserService;

    @Autowired
    AppUtils appUtils;

    public List<ActionDetailVo> getList(String userCode) {


        UserAuthStatusRO userAuthStatus = apiUserService.getUserAuthStatus(userCode);
        AppVo appVo = appUtils.getAppVo();
        AppConfig config = new AppConfig();

        if (appVo != null) {
            config = appVo.getConfig();
        }
        Map<Integer, Integer> statusMap = Maps.newHashMap();
        List<ActionDetailVo> actionDetailVoList = new ArrayList<>();
        for (ActionTypeEnums enums : ActionTypeEnums.values()) {
            ActionDetailVo action = actionListHandler.get(enums).getAction(userCode, config, userAuthStatus);
            action.setActionTypeEnums(enums);
            actionDetailVoList.add(action);
            statusMap.put(enums.getType(), action.getStatus());
        }

        //提示语
        List<ActionDetailVo> actions = Lists.newArrayList();
        for(ActionDetailVo actionDetailVo : actionDetailVoList){
            ActionDetailVo action = new ActionDetailVo();
            BeanUtils.copyProperties(actionDetailVo, action);
            if(Objects.equals(actionDetailVo.getActionTypeEnums(), ActionTypeEnums.PROFILE)){
                actions.add(action);
                continue;
            }
            if(Objects.equals(statusMap.get(ActionTypeEnums.PROFILE.getType()), UserProfileActionStatusEnum.NOT_AUTHORIZED.getCode())){
                action.setEnableToClick(false);
                action.setDisableText("Personal information must be completed first");
            }
            actions.add(action);
        }
        return actions;
    }

}
